import 'dart:async';

import 'package:get/get.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/controllers/device/bind/device_bind_utils.dart';
import 'package:share_fit/app/controllers/device/unbind/device_unbind_utils.dart';
import 'package:share_fit/app/ute/model/device_info.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/app/ute/uteped_device.dart';

import 'device_unbind_state.dart';

class DeviceUnbindLogic extends BaseController implements DeviceChangeListener {
  final DeviceUnbindState state = DeviceUnbindState();
  UTEPedDevice utePedDevice = UTEPedDevice.instance;

  @override
  void onInit() {
    super.onInit();
    utePedDevice.addListener(this);
    addStorageListener(UTEPedConst.keyBattery, (value) {
      state.battery.value = value;
      _updateUI();
    }, global: true);
    _updateUI();
  }

  @override
  void onClose() {
    utePedDevice.removeListener(this);
    super.onClose();
  }

  void onConnectedDeviceTap(DeviceInfo item) {
    item.checked = !item.checked;
    update(["device_list"]);
  }

  void switchDevice(deviceName, address) {
    DeviceBindUtils.showConnectingDialog(deviceName);
    utePedDevice.switchDevice(address);
  }

  void unbindDevice(address) {
    state.unbindAddress.value = address;
    utePedDevice.unbindDevice(address);
  }

  @override
  void onDeviceChangeEvent(event) {
    String address = event[UTEPedConst.keyAddress];
    String action = event[UTEPedConst.keyAction];
    if (action == UTEPedConst.actionDisconnect) {
      if (state.unbindAddress.value.isNotEmpty && state.unbindAddress.value == address) {
        state.unbindAddress.value = "";
        _autoDismissUnbindSuccessDialog();
        DeviceUnbindUtils.showUnbindSuccessDialog();
      }
    }
    else if (action == UTEPedConst.actionConnect) {
      DeviceBindUtils.dismissDialog();
      _updateUI();
    }
    else {
      _updateUI();
    }
  }

  void _autoDismissUnbindSuccessDialog() {
    const timeout = Duration(seconds: 3);
    Timer(timeout, () {
      DeviceUnbindUtils.dismissUnbindSuccessDialog();
      var address = read(UTEPedConst.keyAddress, global: true);
      if (address.toString().isEmpty) {
        Get.back();
      }
      else{
        _updateUI();
      }
    });
  }

  void _updateUI() {
    state.name.value = utePedDevice.deviceInfo.name;
    state.address.value = utePedDevice.deviceInfo.address!;
    state.battery.value = utePedDevice.battery;
    var p = double.tryParse(state.battery.value);
    if (p != null) {
      state.percent.value = p/100;
    }
    state.deviceList.clear();
    for (var device in utePedDevice.deviceList) {
      var deviceInfo = DeviceInfo();
      deviceInfo.from(device);
      state.deviceList.add(deviceInfo);
    }
    update(["device_list", "right_item"]);
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  void initState() {
    // TODO: implement initState
  }

  @override
  void onRemoteEvent(event, method) {

  }
}
